//
//  LSZEmoticonKeyBoard.swift
//  醒浪微博
//
//  Created by 雷顺哲 on 16/7/19.
//  Copyright © 2016年 LSZ. All rights reserved.
//

import UIKit
//  重用标记
private let LSZEmoticonCollectionViewCellIdentifier = "LSZEmoticonCollectionViewCellIdentifier"

class LSZEmoticonKeyBoard: UIView {
    
    // MARK: 懒加载控件
    
    private lazy var toolBar: LSZEmoticonToolBar = {
       let view = LSZEmoticonToolBar(frame: CGRectZero)
        return view
    }()
    
    private lazy var emoticonCollectionView: UICollectionView = {
//        设置布局方式
        let flowLayout = UICollectionViewFlowLayout()
//        设置水平方向滚动
        flowLayout.scrollDirection = .Horizontal
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
//        去掉水平方向和垂直方向的滚动指示条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
//        开启分页模式
        view.pagingEnabled = true
//        去掉弹簧效果
        view.bounces = false
        
        view.backgroundColor = self.backgroundColor
//        注册cell
        view.registerClass(LSZEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: LSZEmoticonCollectionViewCellIdentifier)
//        设置数据源代理
        view.dataSource = self
        return view
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
//        设置键盘的背景
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
//        添加控件
        addSubview(emoticonCollectionView)
        addSubview(toolBar)
        
//        设置约束
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(self.toolBar.snp_top)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.bottom.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(30)
        }
        
//        执行闭包
        toolBar.selectedToolBarButtonClosure = { [weak self] (type: LSZEmoticonToolBarButtonType) in
            
          switch type {
            case .Normal:
                print("normal")
            case .Emoji:
                print("emoji")
            case .Lxh:
                print("浪小花")
            }
            
        }
    }
    
//    重写layoutSubview设置子控件的大小
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
//        每项的大小等于我们表情视图的大小
        flowLayout.itemSize = emoticonCollectionView.size
//        设置水平间距
        flowLayout.minimumInteritemSpacing = 0
//        设置垂直间距
        flowLayout.minimumLineSpacing = 0
        
    }
}


extension LSZEmoticonKeyBoard: UICollectionViewDataSource {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return LSZEmoticonTools.shareTools.allEmoticonArray.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return LSZEmoticonTools.shareTools.allEmoticonArray[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LSZEmoticonCollectionViewCellIdentifier, forIndexPath: indexPath) as! LSZEmoticonCollectionViewCell
        
        cell.emoticons = LSZEmoticonTools.shareTools.allEmoticonArray[indexPath.section][indexPath.item]
        
        return cell
        
    }
}



























